package com.rongshe.web.controller.system;

import com.rongshe.common.core.controller.BaseController;
import com.rongshe.common.core.domain.AjaxResult;
import com.rongshe.common.core.page.TableDataInfo;
import com.rongshe.common.utils.SecurityUtils;
import com.rongshe.system.domain.MessageQueryParam;
import com.rongshe.system.domain.vo.MessageVO;
import com.rongshe.system.service.ISysMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/system/message")
public class SysMessageController extends BaseController {
    @Autowired
    private ISysMessageService messageService;

    // 获取未读消息数量
    @GetMapping("/unread")
    public AjaxResult getUnreadCount() {
        Long userId = SecurityUtils.getUserId();
        int count = messageService.getUnreadCount(userId);
        return AjaxResult.success("操作成功", count);
    }

    // 获取消息列表
    @GetMapping("/list")
    public TableDataInfo list(MessageQueryParam param) {
        startPage();
        Long userId = SecurityUtils.getUserId();
        List<MessageVO> list = messageService.getMessageList(userId, param);
        return getDataTable(list);
    }
    @GetMapping("/info/{messageId}")
    public AjaxResult info(@PathVariable Long messageId) {
        Long userId = SecurityUtils.getUserId();
        MessageVO messageById = messageService.getMessageById(messageId,userId);
        return AjaxResult.success(messageById);
    }

    // 标记消息为已读
    @PutMapping("/read/{messageIds}")
    public AjaxResult markAsRead(@PathVariable Long[] messageIds) {
        Long userId = SecurityUtils.getUserId();
        messageService.markAsRead(userId, messageIds);
        return AjaxResult.success();
    }

    // 批量标记消息为已读
    @PutMapping("/read/batch")
    public AjaxResult markAllAsRead() {
        Long userId = SecurityUtils.getUserId();
        messageService.markAsReadBatch(userId);
        return AjaxResult.success();
    }
}
